<?php
/**
 * Created by PhpStorm.
 * User: guokunsheng
 * Date: 2018/6/6
 * Time: 16:16
 */

namespace app\astrologer\controller\v4;

use app\common\cachemodel\AppConfig;
use app\common\cachemodel\AstroAppVersion;
use app\common\cachemodel\zlcr3\Zlcr3ConsultAccount;
use app\common\lib\Aes;
use app\common\lib\Util;

/**
 * @title App版本更新接口，客户端根据version_code进行是否升级的判断
 * @desc 【附带 控制星盘数据来源为 客户端本地获取或服务器获取 支付方式控制】
 * @url /api/v4/astro_update
 * @version 1.9.8
 * Class AppVersionUpdate
 * @package app\api\controller\v4
 */
class AppUpgrade
{
    /**
     * @title 获取app版本更新信息,根据app型号和版本,若开启强制更新则返回渠道包号
     * @method GET
     * @desc 是否有新版本发布 || 星盘获取方式有变化
     * @return \think\response\Json
     * @throws \Exception
     */
    public function index()
    {
        $headers = request()->header();
        $version_code = input('param.version-code');
        /**
         * 获取当前客户端的最新版本的更新信息
         */
        $AppVersion = (new AstroAppVersion())->getVersionData(['type' => $headers['app-type'], 'version' => ($headers['version'] ?? '')]);

        $appConfig = (new AppConfig())->configInfo('app');

        if ($AppVersion)
        {
            $array = [
                'version' => (int)$AppVersion['version'],
                'version_name' => $AppVersion['version_name'],
                'update_content' => $AppVersion['update_content'],
                'force' => isset($AppVersion['force']) ? (int)$AppVersion['force'] : 0,
                'logo_type' => Util::CheckAppVersion('2.5.2') === '小' ? 1 : (int)$AppVersion['logo_type'],
            ];
            if (strtolower($headers['app-type']) === 'android')
            {
                /**
                 * 判断当前app版本是否需要强制更新,指定更新开启且当前版本包含在内
                 */
                $AppVersionCodeIds = empty($AppVersion['version_force_data']) ? [] : json_decode($AppVersion['version_force_data'], true);
                if ($AppVersion['version_force'] == 1 && in_array($version_code, $AppVersionCodeIds))
                {
                    $array['force'] = 1;
                }
                $array['is_android_check'] = $AppVersion['android_audit_mode_swtich'] ? 1 : 0;
            }
            $array['silent_mode'] = (int)$appConfig['silent_mode'];
            $array['is_consult'] = 0;
            if (!empty($headers['access-token']))
            {
                $accessUserToken = (new Aes())->decryptWithOpenssl($headers['access-token']);
                if (preg_match('/||/', $accessUserToken))
                {
                    [$token, $astrId] = explode('||', $accessUserToken);
                    $consultAccountInfo = (new Zlcr3ConsultAccount())->getInfoById($astrId);
                    if ($consultAccountInfo && $consultAccountInfo['consult_status'] === 1)
                    {
                        $array['is_consult'] = 1;
                    }
                }
            }

            return show(config('code.success'), '加载成功', $array);
        }
        return show(config('code.error'), 'app更新信息获取失败');

    }
}
